<!--

/*
#################################################################################
#
# Copyright (c) 2006 Michigan State University
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#################################################################################

#################################################################################
#
#	Author:
#		Nathan Collins <npcollins@gmail.com>
#
#################################################################################
*/

function stripws(myStr) {
	myStr = String(myStr);
	return myStr.replace(/^\s*|\s*$/g,""); 
}

function parse_f(field,data,mod) {
	retval = '';
	if (data.length > 1) {
		retval += ' ' + field + ':"' + data + '"';
		if (mod != '1') {
			retval += '^' + mod;
		}
	}
	return retval;
}

function gen_search() {
	var final_search = '';
	/* keywords */
	var keywords = document.creator.keywords.value;
	final_search += keywords;

	/* phrase */
	var phrase = stripws(document.creator.phrase.value);
	if (phrase.length > 1) {
		final_search += ' "' + phrase + '"';
	}

	/* excude these */
	var exclude = stripws(document.creator.exclude.value);
	if (exclude.length > 1) {
		var temp = new Array();
		temp = exclude.split(' ');
		for (i = 0; i < temp.length; i++) {
			final_search += ' -' + temp[i];
		}
	}

	/* limit by repository */
	var repositLimit = stripws(document.creator.repository.value);
	if (repositLimit.length > 1) {
		final_search += ' rep:"' + repositLimit + '" ';
	}

	/* limit by language */
	var langLimit = stripws(document.creator.language.value)
	if (langLimit.length > 1) {
		final_search += ' ll:"' + langLimit + '" ';
	}
	
	/* words close to each other */
	var nearby = stripws(document.creator.nearby.value);
	if (nearby.length > 1) {
		final_search += ' "' + nearby + '"~8';		/* within 8 words */
	}

	/* valid url */
	var valurl = get_radio_value(document.creator.valurl);
	if (valurl == 'n') {
		final_search += ' lu:1';
	}

	var title = stripws(document.creator.title.value);
	var creator = stripws(document.creator.creator.value);
	var subject = stripws(document.creator.subject.value);
	var title_mod = get_radio_value(document.creator.titlemod);
	var creator_mod = get_radio_value(document.creator.creatormod);
	var subject_mod = get_radio_value(document.creator.subjectmod);

	/* fields */
	final_search += parse_f('title',title,title_mod);
	final_search += parse_f('author',creator,creator_mod);
	final_search += parse_f('subject',subject,subject_mod);

	/* pass the final_search search to the search box */
	searchstr = stripws(final_search);
	
	if (final_search.length > 1) {
		window.location = '/search.py?s='+searchstr
	}
	else {
		alert('Please enter a search first.');
	}
	return false;
}

//-->

